// pages/rate/rate.js
const app = getApp()
var network = require("../../utils/request.js"); //网络请求
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    "firstList": [{ name: 'w券1', money: '5.00' }, { name: 'w券2', money: '50.00' }],
    "secondList": [{ name: 'y券1', money: '10.00' }, { name: 'y券2', money: '20.00' }],
    "thirdList": [{ name: 'g券1', money: '30.00' }, { name: 'g券2', money: '40.00' }],
    total: 0,
    isLoging: false,
    noData: "../../resource/images/noData.png",//无数据图片
    query: {
      pageSize: 10,
      pageNum: 1
    },
    items: [], //列表数据源
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      'title':"我的收藏"
    })
    this.loading()
  },
  //获取数据
  loading(query = {}) {
    Object.assign(this.data.query, query)
    network.post('queryMyProvision', this.data.query).then(resp => {
      console.log(resp)
      this.data.items.push(...resp.rows)
      this.setData({
        items: this.data.items,
        total: resp.total,
        isLoging: false,
      })
      console.log(this.data.items)
    })
  },

  //swiper切换时会调用
  pagechange: function (e) {
    if ("touch" === e.detail.source) {
      let currentPageIndex = this.data.currentIndex
      currentPageIndex = (currentPageIndex + 1) % 3
      this.setData({
        currentIndex: currentPageIndex
      })
    }
  },
  //用户点击tab时调用
  titleClick: function (e) {
    let currentPageIndex =
      this.setData({
        //拿到当前索引并动态改变
        currentIndex: e.currentTarget.dataset.idx
      })
  },


  
  //下拉事件
  bindPickerChangeOne: function (e) {
    this.setData({
      indexOne: e.detail.value
    })
    this.data.query.pageNum = 1
    this.data.items = []
    var standard = this.data.arrayOne[e.detail.value].standard
    this.loading({
      "standard": standard
    })

  },


  lookInfo(e){
    //查看详情
    var id = this.data.items[e.currentTarget.dataset.index].id
    wx.navigateTo({
      url: "purchaseManageDetail/purchaseManageDetail?id=" + id
    });
  },

  onPullDownRefresh() {
    this.data.query.pageNum = 1;
    this.data.items = [];
    this.loading()
    wx.pageScrollTo({
      scrollTop: 0
    })
    wx.stopPullDownRefresh()
  },

  onReachBottom() {
    //加载更多
    var total = parseInt(this.data.total)
    var pageNum = this.data.query.pageNum
    var pageCount = Math.ceil(total / this.data.query.pageSize)
    console.log(total, pageNum)
    if (pageNum < pageCount && !this.data.isLoging) {
      this.data.isLoging = true
      this.data.query.pageNum = ++pageNum
      this.loading()
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '跑合中药材供需云平台',
      imageUrl: '../../resource/images/share.jpg',
      path: 'pages/supplyList/supplyList',
      success: function (res) {
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享取消',
        })
      }
    }
  }
})